package com.jumi.microservice.service.order;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.domain.ExpressDataDTO;
import com.jumi.microservice.dto.ExpressResponse;
import com.jumi.microservice.dto.admin.*;
import com.jumi.microservice.dto.apiService.OrderGoodsResponse;
import com.jumi.microservice.dto.apiService.OrderInformationResponse;
import com.jumi.microservice.dto.order.*;
import com.jumi.microservice.dto.serviceDto.*;
import com.jumi.microservice.mapper.OrderGoodsMapper;
import com.jumi.microservice.mapper.UserOrderMapper;
import com.jumi.microservice.service.BasicDataApi;
import com.jumi.microservice.service.IOrderDubboService;
import com.jumi.microservice.service.IOrderService;
import com.jumi.microservice.service.logistics.admin.SendService;
import com.jumi.microservice.service.logistics.express.KdApiOrderDistinguish;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author by mazhao
 * @Classname OrderInnerDubboServiceImpl
 * @Description TODO
 * @Date 2020/10/23 9:34
 */
@DubboService(interfaceClass = IOrderService.class)
public class OrderInnerDubboServiceImpl implements IOrderService {

    private static final Logger log = LoggerFactory.getLogger(OrderInnerDubboServiceImpl.class);

    @Resource
    OrderServiceImpl orderServiceImpl;
    @Resource
    AllOrderServiceImpl allOrderServiceImpl;
    @DubboReference
    IOrderDubboService orderDubboService;
    @Resource
    SendService sendService;
    @Resource
    UserOrderMapper userOrderMapper;
    @DubboReference
    BasicDataApi iExpressService;

    @Override
    public List<Integer> getOrderBySupplierId(int supplierId) {
        return orderServiceImpl.getOrderBySupplierId(supplierId);
    }

    @Override
    public List<Integer> getOrderByBuyerId(int buyerId) {
        return orderServiceImpl.getOrderByBuyerId(buyerId);
    }

    @Override
    public OrderGoodsEntityResponse getOrderGoodsDetails(OrderIdGoodsIdRequest orderIdGoodsIdRequest) {
        return orderServiceImpl.getOrderGoodsDetails(orderIdGoodsIdRequest);
    }

    @Override
    public List<OrderInformationResponse> getOrderInformation(int[] orderIdArr) {
        return orderServiceImpl.getOrderInformation(orderIdArr);
    }

    @Override
    public List<OrderGoodsResponse> getGoodsIdAndGoodsName(int[] goodsIdArr) {
        return orderServiceImpl.getGoodsIdAndGoodsName(goodsIdArr);
    }

    @Override
    public Boolean deliverGoods(DeliverGoodsRequest request) {
        return orderDubboService.deliverGoods(request);
    }

    @Override
    public Boolean updateOrderStatus(int orderId, byte orderStatus) {
        return orderServiceImpl.updateOrderStatus(orderId, orderStatus);
    }

    @Override
    public Boolean cancelLogistics(Integer orderId) {
        return sendService.cancelLogistics(orderId);
    }

    @Override
    public SalesResponse getSpuSalesVolume(SalesRequest salesRequest) {
        return orderDubboService.getSpuSalesVolume(salesRequest);
    }

    @Override
    public List<Map<String, Object>> selfSave(String uids) {
        return userOrderMapper.selfSave(uids);
    }

    @Override
    public ResponseResult<InvoiceGoodsResponse> getInvoiceGoodsResponse(String orderNo) {
        ResponseResult responseResult = new ResponseResult();
        return responseResult.setData(allOrderServiceImpl.getInvoiceGoodsResponse(orderNo));
    }

    @Override
    public Integer countUserOrder(UserOrderRequest request) {
        return orderDubboService.countUserOrder(request);
    }

    @Override
    public List<OrderLogResponse> getOrderLogList(String orderSn) {
        return orderDubboService.getOrderLogList(orderSn);
    }

    @Override
    public List<OrderResponse> getOrderInfo(OrderInfoRequest request) {
        return orderDubboService.getOrderInfoList(request);
    }

    @Override
    public Integer waitCommentCount(Long uid) {
        return orderDubboService.waitCommentCount(uid);
    }

    @Override
    public List<ActivityUserSalesRequest> getSpuSalesVolumeActivity(List<ActivityUserSalesRequest> requestList) {
        return orderDubboService.getSpuSalesVolumeActivity(requestList);
    }

    @Override
    public OrderDetailsResponse getOrderDetails(OrderDetailsRequest orderDetailsRequest) throws Exception {
        return allOrderServiceImpl.getOrderDetails(orderDetailsRequest);
    }

    @Override
    public List<Integer> getOrderByRealWarehouseId(int realWarehouseId) {
        return orderServiceImpl.getOrderByRealWarehouseId(realWarehouseId);
    }

    @Override
    public List<Integer> getOrderByBeginTime(String beginTime) {
        return orderServiceImpl.getOrderByBeginTime(beginTime);
    }

    @Override
    public List<Integer> getOrderByEndTime(String endTime) {
        return orderServiceImpl.getOrderByEndTime(endTime);
    }

    @Override
    public List<Integer> getOrderByGoodsName(String goodsName) {
        return orderServiceImpl.getOrderByGoodsName(goodsName);
    }

    @Override
    public List<Integer> getOrderByPhone(String phone) {
        return orderServiceImpl.getOrderByPhone(phone);
    }

    @Override
    public Integer getOrderStatus(int orderId) {
        return orderServiceImpl.getOrderStatus(orderId);
    }

    @Override
    public OrderStatusAfterSealResponse getStatusAfterSeal(int orderId, int goodsId) {
        return orderServiceImpl.getStatusAfterSeal(orderId, goodsId);
    }

    @Override
    public Boolean updateAfterSealStatus(UpdateAfterSealStatusRequest updateAfterSealStatusRequest) {
        return orderServiceImpl.updateAfterSealStatus(updateAfterSealStatusRequest);
    }

    @Override
    public Boolean updateReturnInfo(UpdateReturnInfoRequest updateReturnInfoRequest) {
        return orderServiceImpl.updateReturnInfo(updateReturnInfoRequest);
    }

    @Override
    public Boolean updateRefundInfo(UpdateRefundInfoRequest updateRefundInfoRequest) {
        return orderServiceImpl.updateRefundInfo(updateRefundInfoRequest);
    }

    @Override
    public Boolean updateRefundGoodsInfo(UpdateRefundGoodsInfoRequest updateRefundGoodsInfoRequest) {
        return orderServiceImpl.updateRefundGoodsInfo(updateRefundGoodsInfoRequest);
    }

    @Override
    public Integer realSendAmount(int orderId, int goodsId) {
        return orderServiceImpl.realSendAmount(orderId, goodsId);
    }

    @Override
    public List<Integer> getOrderIdByCreateTime(Date payTime, Date payEndTime) {
        return orderServiceImpl.getOrderIdByCreateTime(payTime, payEndTime);
    }

    @Override
    public OrderGoodsServiceDetailsResponse findOrderGoodsDetails(OrderGoodsServiceDetailsRequest request) {
        return orderServiceImpl.findOrderGoodsDetails(request);
    }

    @Override
    public Date getConfirmTime(int orderId) {
        return orderServiceImpl.getConfirmTime(orderId);
    }

    @Override
    public Boolean isExistAfterSale(int orderId) {
        return orderServiceImpl.isExistAfterSale(orderId);
    }

    @Override
    public Boolean cancelLogisticsAndGoods(Integer orderId, Integer goodsId) {
        return sendService.cancelLogisticsAndGoods(orderId, goodsId);
    }

    @Override
    public Boolean orderGoodsStatus(int orderId) {
        return orderServiceImpl.orderGoodsStatus(orderId);
    }

    @Override
    public ExpressResponse autoIdentify(String logisticsNo) {
        log.debug("快递鸟自动识别单号参数，快递单号[{}]",logisticsNo);
        KdApiOrderDistinguish kdApiOrderDistinguish = new KdApiOrderDistinguish();
        ExpressResponse expressResponse = new ExpressResponse();
        try {
            String res = kdApiOrderDistinguish.getOrderTracesByJson(logisticsNo);
            log.debug("快递鸟自动识别快递单号返回数据[{}]",res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            if (jsonObject.getBoolean("Success")) {
                Map<String, String> expressMap = expressInfo();
                JSONArray array = jsonObject.getJSONArray("Shippers");
                for (Object o : array) {
                    JSONObject json = JSONObject.parseObject(o.toString());
                    expressResponse.setExpressCode(json.getString("ShipperCode"));
                    expressResponse.setExpressName("");
                    if (expressMap.containsKey(json.getString("ShipperCode"))) {
                        expressResponse.setExpressName(expressMap.get(json.getString("ShipperCode")));
                    }
                    return expressResponse;
                }
            } else {
                throw new BaseException(500, jsonObject.getString("Reason"));
            }
        } catch (Exception e) {
            log.error("自动识别单号 错误{}", e.getMessage());
        }
        return expressResponse;
    }

    @Override
    public Boolean updateInvoiceId(String orderNo, Integer invoiceId) {
        return orderServiceImpl.updateInvoiceId(orderNo, invoiceId);
    }

    /**
     * 商品出库中弹窗提示接口
     *
     * @param orderId
     * @param skuIdStr
     * @return
     */
    @Override
    public Boolean isDelivery(Integer orderId, String skuIdStr) {
        return orderServiceImpl.isDelivery(orderId, skuIdStr);
    }

    /**
     * 根据支付单号统计多个订单的总支付金额
     * 一个支付单号--对应多个订单
     *
     * @param payNo
     * @return
     */
    @Override
    public BigDecimal getPayTotalAmount(String payNo) {
        return orderServiceImpl.getPayTotalAmount(payNo);
    }

    /**
     * 快递公司信息
     *
     * @return
     */
    public Map<String, String> expressInfo() {
        return iExpressService.selectExpressList()
                .stream().filter(e -> e.getExpressCode() != null)
                .collect(Collectors.toMap(ExpressDataDTO::getExpressCode, ExpressDataDTO::getExpressName));
    }

}
